********************************************************************************
****************                  Table A13                        **************
********************************************************************************
use "C:\Users\junok\Dropbox\replication files\data sets\QSW_Undervaluation_Models_2022Nov23",clear
*use "C:\Users\quinnd\Dropbox\XR Politics\replication files\data sets\QSW_Undervaluation_Models_2022Nov23"


cd "C:\Users\junok\Dropbox\replication files\IO_Replication_July2023_JK\Part 3 Replication - July 2023"



********************************************************************************
drop if banking_center==1
drop if country=="Iraq"
drop if country=="El Salvador"
drop if country=="Mozambique"
drop if country=="Iran"
drop if country=="Syria" 
drop if country=="Lebanon"
drop if country=="Zimbabwe"
drop if wbcode=="BMU"
drop if wbcode=="VCT"
drop if wbcode=="LBR"

********************************************************************************
label var country "Country"
label var years "Year"
label var underval "Exchange Rate Undervaluation (Mean)"
label var wbcode "World Bank Country Code"
label var predict "Predict"
label var predict7089 "Predict"
label var predict9017 "Predict"
label var predict91 "Predict"
label var underval91 "Exchange Rate Unvervaluation_91"
label var lgrow9 "Log Growth"
label var balance9 "Trade/GDP Trade Balance"
label var popgr9 "Population Growth (mean)"
label var trade_gdp "Trade/GDP"
label var d1_d5 "Ratio between 1st Decile and 5th Decile"
label var ln_infl "Log Inflation (mean)"
label var inflation "Inflation (mean)"
label var inflation_gdpdeflator "Inflation GDP Deflator (mean)"
label var unemployment_ilo_est "Unemployment ILO Estimate (mean)"
label var unemployment_nat_est "Unemployment National Estimate (mean)"
label var capinnonresident "Capital Inflow Restrictions (mean)"
label var capoutresident "Capital Outflow Restrictions (mean)"
label var cap100 "Capital Account (mean)"
label var cur100 "Financial Current Account (mean)"
label var polity2 "Polity 2 (mean)"
label var polcomp "Political Competition (mean)"
label var resource_rents "Resource Rents"
label var mean_polity_g "Polity score (mean)"
label var mean_polcomp_g "Political Competition score (mean)"
label var exconst "Executive Constraints"
label var h_polcon3 "Political Constraints Index III"
label var fh_civ "Civil Liberties Rating (Freedom House)"
label var fh_pol "Political Rights Rating"
label var execrlc "Chief Executive Party Orientation"
label var pr "Proportional Representation"
label var worker "Worker Rights"
label var lvau "De Jure and De Facto Central Bank Independence Index unweighted"
label var lvaw "De Jure and De Facto Central Bank Independence Index weighted"
label var age_dependency_ratio "Age Dependency Ratio"
label var underval_sd "Standard Deviation Exchange Rate Undervaluation"
label var lgrow9_sd "Standard Deviation Log Growth"
label var index "Country group index"
label var polcomp_dummy "Political Competition (Dummy)"
label var polity2_4 "Polity2_4"
label var polity2_3 "Polity 2_3"
label var banking_center "Banking Center"
label var euro "Monetary Union"
label var EU "European Union membership"
label var ifscode "International Financial Statistics Database Country Code"
label var exchangerateregime_fine "Exchange Rate Regime Rheinart and Rogoff (Fine classification)"
label var exchangerateregime_coarse "Exchange Rate Regime (Coarse Classification)"
label var base "ifs code of base country"
label var range "Range of Exchange Rate Against Base (in percent)"
label var numzero "Months with zero change in exchange rate"
label var pegtype "Peg Type 1 = 0% change, 2 = 1% band, 3 = 2% band, 4 = 1 time deval/reval (Shambaugh)"
// label var peg "Standard Shambaugh 2004 classification (1= peg,0= nonpeg) (Shambaugh)"
label var peg "20221019 DQJK updated Shambaugh peg"
label var sypeg "Single Year Peg (Shambaugh)"
label var sypeg "Shambaugh Updated single year peg"
label var spegtype "Single Year Peg Type (same rules as pegtype) (Shambaugh)"
label var maxde "Largest (or most positive) % monthly change vs base (Shambaugh)"
label var minde "Smallest (or most negative) % monthly change vs base (Shambaugh)"
label var softpeg "Soft Peg Classification from OST AEJ-macro 2010 (Shambaugh)"
label var softpegtype "Soft Peg Type (Shambaugh)"
label var evol "Standard Deviation of Percentage Change in the Exchange Rate (Shambaugh)"
label var kspeg "Klein Shambaugh Classification (peg+sypeg - type 4) see JIE 2008"
label var oecd "OECD"
label var euro "20221019 DQJK updated euro"
label var peg_no_euro "pegnoeuro=1 if (peg==1 & euro==0) - 20221019 DQJK updated"


save "C:\Users\junok\Dropbox\juno\five year files\QSW_Undervaluation_Models_2022Nov23.dta" , replace

********************************************************************************

**# Bookmark #4

xtset index year, delta(5)
replace NTMCoverageratioExport = NTMCoverageratioExport /100
replace NTMCoverageratioImport = NTMCoverageratioImport /100
// gen fh_combined = fh_civ+fh_pol
// gen common=.
// replace common=1 if exconst!=. & h_polcon3!=. & fh_combined!=. & cap100!=.


eststo clear



**********              1. Ex Constrain 		  **********
eststo: xtreg underval91   i.year  l.underval91   l.lnrealgdp9 l.lgrow9 l.peg  l.balance9 l.ltrade9 l.inflation   l.age l.polcomp l.exconst if l.common==1 , vce (cluster index)

**********              2. Polcon 		  **********
eststo: xtreg underval91   i.year  l.underval91   l.lnrealgdp9 l.lgrow9 l.peg  l.balance9 l.ltrade9 l.inflation   l.age l.polcomp l.h_polcon3 if l.common==1 , vce (cluster index)

**********              3. Freedom House (Combined) 		  **********
eststo: xtreg underval91   i.year  l.underval91   l.lnrealgdp9 l.lgrow9 l.peg  l.balance9 l.ltrade9 l.inflation   l.age l.polcomp l.fh_combined if l.common==1  , vce (cluster index)

**********              4. Capital Open 		  **********
eststo: xtreg underval91   i.year  l.underval91   l.lnrealgdp9 l.lgrow9 l.peg  l.balance9 l.ltrade9 l.inflation   l.age l.polcomp l.cap100 if l.common==1 , vce (cluster index)

**********              5. 1-4 		  **********
eststo: xtreg underval91   i.year  l.underval91   l.lnrealgdp9 l.lgrow9 l.peg  l.balance9 l.ltrade9 l.inflation   l.age l.polcomp l.exconst l.h_polcon3 l.fh_combined   l.cap100  if l.common==1 , vce (cluster index)


**********              6. CBI 		  **********
*For CBI, further specify common sample
// replace common=1 if lvau!=.

eststo: xtreg underval91   i.year  l.underval91   l.lnrealgdp9 l.lgrow9 l.peg  l.balance9 l.ltrade9 l.inflation   l.age l.polcomp l.lvau  , vce (cluster index)


**********              7. Capital Open & CBI 		  **********
eststo: xtreg underval91   i.year  l.underval91   l.lnrealgdp9 l.lgrow9 l.peg  l.balance9 l.ltrade9 l.inflation   l.age l.polcomp l.lvau l.cap100 , vce (cluster index)

**********              8. NTM Import Coverage 		  **********
eststo: xtreg underval91   i.year  l.underval91   l.lnrealgdp9 l.lgrow9 l.peg  l.balance9 l.ltrade9 l.inflation   l.age l.polcomp l.NTMCoverageratio* if l.common==1 , vce (cluster index)


esttab * using Table_A13.csv, replace label  compress b(%9.3f) se(3) starlevels(* 0.10 ** 0.05 *** 0.01) sca(N N_clust r2_w r2_o) sfmt(%9.0f %9.0f %9.3f ) nomtitles  nonotes noobs 







//////////////
//Durbin's M / 
//////////////
eststo clear

**********              1. Ex Constrain 		  **********
xtreg underval91   i.year  c.l.polcomp l.exconst		l.underval91  l.peg l.lnrealgdp9 l.lgrow9  l.balance9 l.ltrade9 l.inflation  age if l.common==1 , vce (cluster index)

//Durbin's M
predict predict1 if e(sample), xb
gen resid1 = underval91 - predict1 if e(sample)
xtreg underval91   i.year  c.l.polcomp l.exconst l.underval91  l.peg l.lnrealgdp9 l.lgrow9  l.balance9 l.ltrade9 l.inflation   age l.resid1 if l.common==1 , vce (cluster index)
//Durbin's M (p-value):  0.083

// re-run the code including the lagged residual and see the p-value of it. If p-value is, say less than 0.05, then there could be somethings that lagged endogenous variable should have captured but left over; AR(1) process is not enough or not appropriate?

**********              2. Polcon 		  **********
eststo clear
xtreg underval91   i.year  c.l.polcomp l.h_polcon3		l.underval91  l.peg l.lnrealgdp9 l.lgrow9  l.balance9 l.ltrade9 l.inflation   age if l.common==1 , vce (cluster index)

//Durbin's M
predict predict2 if e(sample), xb
gen resid2 = underval91 - predict2 if e(sample)
xtreg underval91   i.year  c.l.polcomp l.h_polcon3		l.underval91  l.lnrealgdp9 l.lgrow9  l.balance9 l.ltrade9 l.inflation  l.peg age l.resid2 if l.common==1 , vce (cluster index)
//Durbin's M (p-value):  0.092



**********              3. Freedom House (Combined) 		  **********
eststo clear
xtreg underval91   i.year  c.l.polcomp l.fh_combined		l.underval91  l.peg l.lnrealgdp9 l.lgrow9  l.balance9 l.ltrade9 l.inflation  age if l.common==1 , vce (cluster index)

//Durbin's M
predict predict3 if e(sample), xb
gen resid3 = underval91 - predict3 if e(sample)
eststo: xtreg underval91   i.year  c.l.polcomp l.fh_combined		l.underval91  l.peg l.lnrealgdp9 l.lgrow9  l.balance9 l.ltrade9 l.inflation  age l.resid3 if l.common==1 , vce (cluster index)
//Durbin's M (p-value):  0.089


**********              4. Capital Open 		  **********
eststo clear
xtreg underval91   i.year  c.l.polcomp l.cap100		l.underval91  l.peg l.lnrealgdp9 l.lgrow9  l.balance9 l.ltrade9 l.inflation   age if l.common==1 , vce (cluster index)

//Durbin's M
predict predict4 if e(sample), xb
gen resid4 = underval91 - predict4 if e(sample)
eststo: xtreg underval91   i.year  c.l.polcomp l.cap100		l.underval91  l.peg l.lnrealgdp9 l.lgrow9  l.balance9 l.ltrade9 l.inflation  age l.resid4 if l.common==1 , vce (cluster index)
//Durbin's M (p-value):  0.177 

**********              5. 1-4 		  **********
eststo clear
xtreg underval91   i.year  c.l.polcomp l.exconst l.h_polcon3 l.fh_combined l.cap100			l.underval91  l.peg l.lnrealgdp9 l.lgrow9  l.balance9 l.ltrade9 l.inflation   age if l.common==1 , vce (cluster index)
//Durbin's M
predict predict5 if e(sample), xb
gen resid5 = underval91 - predict5 if e(sample)
xtreg underval91   i.year  c.l.polcomp l.exconst l.h_polcon3 l.fh_combined l.cap100			l.underval91  l.peg l.lnrealgdp9 l.lgrow9  l.balance9 l.ltrade9 l.inflation   age l.resid5 if l.common==1 , vce (cluster index)
//Durbin's M (p-value):  0.203


**********              6. CBI 		  **********
*For CBI, further specify common sample
replace common=1 if lvau!=.


eststo clear
xtreg underval91   i.year  c.l.polcomp l.lvau		l.underval91  l.peg l.lnrealgdp9 l.lgrow9  l.balance9 l.ltrade9 l.inflation   age if l.common==1 , vce (cluster index)
//Durbin's M
predict predict6 if e(sample), xb
gen resid6 = underval91 - predict6 if e(sample)
xtreg underval91   i.year  c.l.polcomp l.lvau		l.underval91  l.peg l.lnrealgdp9 l.lgrow9  l.balance9 l.ltrade9 l.inflation   age  l.resid6 if l.common==1, vce (cluster index)
//Durbin's M (p-value):  0.079

**********              7. Capital Open & CBI 		  **********
eststo clear
xtreg underval91   i.year  c.l.polcomp l.cap100 l.lvau		l.underval91  l.peg l.lnrealgdp9 l.lgrow9  l.balance9 l.ltrade9 l.inflation   age if l.common==1 , vce (cluster index)
//Durbin's M
predict predict7 if e(sample), xb
gen resid7 = underval91 - predict7 if e(sample)
xtreg underval91   i.year  c.l.polcomp l.cap100 l.lvau		l.underval91  l.peg l.lnrealgdp9 l.lgrow9  l.balance9 l.ltrade9 l.inflation   age l.resid7 if l.common==1 , vce (cluster index)
//Durbin's M (p-value):  0.200

